package com.cloudz.iptv.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.cloudz.iptv.App;
import com.owen.focus.FocusBorder;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @File Name: com.cloudz.iptv.fragment
 * @Author: YJ //作者及
 * @Date: 2018-12-12 16:15//完成日期
 * @Description: // 基础BaseFragment
 * @Version: v0.0.1 // 版本信息
 * @Function List: // 主要函数及其功能
 * @Others: // 其它内容的说明
 * @History: // 历史修改记录
 */
public abstract class BaseFragment extends Fragment
{

    protected FocusBorder focusBorder;

    private Unbinder unbinder;

    protected Toast toast;

    public abstract int getLayoutId();

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if(getActivity() instanceof FocusBorderHelper) {
            focusBorder = ((FocusBorderHelper)getActivity()).getFocusBorder();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =  inflater.inflate(getLayoutId(), container, false);
        //初始化注解插件
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        final Activity activity = getActivity();

        toast = Toast.makeText(activity, "", Toast.LENGTH_SHORT);
        //设置对齐方式
        toast.setGravity(Gravity.CENTER, 0, 0);
    }

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        super.onDestroyView();
    }

    public void showToast(String str) {
        toast.setText(str);
        toast.show();
    }

    protected void onMoveFocusBorder(View focusedView, float scale)
    {
        if (null != focusBorder)
        {
            focusBorder.onFocus(focusedView, FocusBorder.OptionsFactory.get(scale, scale));
        }
    }

    protected void onMoveFocusBorder(View focusedView, float scale, float roundRadius)
    {
        if (null != focusBorder)
        {
            focusBorder.onFocus(focusedView, FocusBorder.OptionsFactory.get(scale, scale, roundRadius));
        }
    }

//    public Context getContext() {
//        if (activity == null) {
//            return App.get();
//        }
//        return activity;
//    }

    public interface FocusBorderHelper {
        FocusBorder getFocusBorder();
    }
}
